<?php
/**
 * User: zhangyu
 * Date: 2022/5/9
 * Time: 10:31
 */

declare (strict_types = 1);


namespace app\admin\services;

class ExcelService
{
    /**
     * 导出单sheet excel
     * @param array $fields 字段, eg: ['字段1', '字段2'...]
     * @param array $data 数据
     * @param string|null $filename 导出文件名
     * @return bool
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */

    public static function makeExcel(array $fields = [], array $data = [], $file = "01simple")
    {
        if (!is_array($fields) || !is_array($data)) {
            return false;
        }
        $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        // 方法二，使用 setCellValue
        //表头
        //设置单元格内容
        $titCol = 'A';
        foreach ($fields as $key => $value) {
            // 单元格内容写入
            $sheet->setCellValue($titCol . '1', $value);
            $sheet->getColumnDimension($titCol)->setWidth(20);
            $titCol++;
        }
        $row = 2; // 从第二行开始
        foreach ($data as $item) {
            $dataCol = 'A';
            foreach ($item as $value) {
                // 单元格内容写入
                $sheet->setCellValue($dataCol . $row, $value);
                $dataCol++;
            }
            $row++;
        }
        ob_end_clean();
        // Redirect output to a client’s web browser (Xlsx)
        header('Content-Type: applicationnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $file . '.xlsx"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0

        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
        exit;
    }

    /**
     * @param $data_array [fields,sheetName,data]
     * @param $filename 文件名
     * @return void
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
    public static function makeExcelWithMoreSheet($data_array, $filename){
        $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
        foreach ($data_array as $key=>$item) {
            self::opSheet($key,$spreadsheet,$item['sheetName'],$item['fields'],$item['data']);
        }
        ob_end_clean();
        header('Content-Type: applicationnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0

        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
    }

    /**
     * @param $spreadsheet
     * @param $sheetName
     * @param $fields
     * @param $data
     * @return void
     */
    public static function opSheet($key,$spreadsheet,$sheetName,$fields ,$data)
    {
        $spreadsheet->createSheet();//创建sheet
        $spreadsheet->setActiveSheetIndex($key);
        $sheet = $spreadsheet->getActiveSheet($key)->setTitle($sheetName);//设置sheet的名称

        $titCol = 'A';
        foreach ($fields as  $value) {
            // 单元格内容写入
            $sheet->setCellValue($titCol . '1', $value);
            $sheet->getColumnDimension($titCol)->setWidth(20);
            $titCol++;
        }
        $row = 2; // 从第二行开始
        foreach ($data as $item) {
            $dataCol = 'A';
            foreach ($item as $value) {
                // 单元格内容写入
                $sheet->setCellValue($dataCol . $row, $value);
                $dataCol++;
            }
            $row++;
        }
    }


}
